package com.yangoxix.lovegoing.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yangoxix.lovegoing.mapper.ImageMapper;
import com.yangoxix.lovegoing.mapper.UserMapper;
import com.yangoxix.lovegoing.page.CommentPage;
import com.yangoxix.lovegoing.page.StatePage;
import com.yangoxix.lovegoing.pojo.Comment;
import com.yangoxix.lovegoing.pojo.Image;
import com.yangoxix.lovegoing.pojo.State;
import com.yangoxix.lovegoing.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: lovegoing
 * @description: state的service
 * @author: chenzou
 * @create: 2021-09-22 14:46
 **/
@Service
public class StateService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ImageMapper imageMapper;
    //    把state数据传入数据库
    public boolean insertState(State data) {

        try {
            data.insert();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }


    }

    //    查询数据库是否有相同的数据
    public boolean selectSomeStateByTitle(String titleText) {
        State state = new State();
        QueryWrapper<State> wr = new QueryWrapper<>();
        wr.eq("title", titleText);
        try {
            State state1 = state.selectOne(wr);
            return state1 == null;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    //   把img数据传入数据库
    public boolean insertImg(Image data) {
        try {
            data.insert();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    //  获取全部state的信息
    public List<StatePage> getAllStateInfo() {
        State state = new State();
        List<State> states = state.selectAll();
        List<StatePage> statePages = new ArrayList<>();
        for (State s : states) {
            StatePage statePage = new StatePage();
            statePage.setState(s);
            statePage.setImages(getDataFromImg(s.getTitle()));
            statePages.add(statePage);
            statePage.setUserimg(getportrait(s.getPublisher()));
        }
        return statePages;
    }

//    根据name获取头像
    public String getportrait(String name){
        User user = new User();
        User find = user.selectById(name);
        if (find == null) {
            return null;
        }
        return find.getPhoto();
    }

    //   根据name从image表中获取数据
    public List<Image> getDataFromImg(String name) {
        Image image = new Image();
        QueryWrapper<Image> wr = new QueryWrapper<>();
        QueryWrapper<Image> activityname = wr.eq("activityname", name);
        List<Image> images = image.selectList(wr);
        return images;
    }

//    获取一个state的全部信息
    public State getStateDetail(String name){
        State state = new State();
        QueryWrapper<State> stateQueryWrapper = new QueryWrapper<>();
        stateQueryWrapper.eq("title", name);
        State findedState = state.selectOne(stateQueryWrapper);
        return findedState;
    }

    //    获取comment的全部信息
    public List<Comment> getStateComment(int id) {
        Comment comment = new Comment();
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("stateid", id);
        List<Comment> comments = comment.selectList(wrapper);
        return comments;
    }

//    通过title获取state的id
    public int getIdByTitle(String title){
        State state = new State();
        QueryWrapper<State> stateQueryWrapper = new QueryWrapper<>();
        State findedState = state.selectOne(stateQueryWrapper.eq("title", title));
        int id = findedState.getId();
        return id;
    }

//    获取动态内的全部评论
    public ArrayList<CommentPage> getAllCommmentInState(String title){
        int idByTitle = getIdByTitle(title);
        List<Comment> stateComment = getStateComment(idByTitle);
        ArrayList<CommentPage> commentPages = new ArrayList<>();
        for (Comment comment : stateComment) {
            CommentPage commentPage = new CommentPage();
            commentPage.setComment(comment);
            User user = new User();
            User user1 = user.selectById(comment.getUsername());
            String imageurl = user1.getPhoto();
            commentPage.setUserImageUrl(imageurl);
            commentPages.add(commentPage);
        }
        return commentPages;
    }

//   上传头像
    public boolean uploadPotrait(String username,String imageUrl){
        try {
            User user = new User();
            UpdateWrapper<User> wrapper = new UpdateWrapper<>();
            wrapper.eq("username", username).set("photo", imageUrl);
            userMapper.update(null, wrapper);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    //    删除已存在的头像
    public void deletePotrait(String name) {
        QueryWrapper<Image> wrapper = new QueryWrapper<>();
        wrapper.eq("imgurl", name);
        imageMapper.delete(wrapper);
    }

//  comment数据传入数据库
    public boolean insertComment(Comment comment){
        try {
            comment.insert();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
